<?php

namespace app\common\helper;

use think\helper\Str;

class StrHelper
{

    /**
     * 根据控制器名称，生成菜单名称
     * @param $controller
     * @return array
     */
    public static function getMenuNames($controller): array
    {
        // 去掉前后的斜杠
        $normalized = trim($controller, '/');
        // 分割到数组，大驼峰命名
        $parts = explode('/', $normalized);
        // 分割到数组，小写中划线命名
        $kebabParts = array_map(function ($part) {
            return str_replace('_', '-', Str::snake($part));
        }, $parts);
        // 前加斜杠的小写中画线命名
        $routePathFull = '/' . implode('/', $kebabParts);
        // 驼峰命名
        $routeName = implode('', array_map('ucfirst', $parts));
        // 当前模块路径 控制器去掉最后一部分，前面的目录部分
        $path_parts = array_slice($kebabParts, 0, -1);
        $module_path = $path_parts ? implode(DIRECTORY_SEPARATOR, $path_parts) : '';
        $module_namespace = $path_parts ? implode("\\", $path_parts) : '';

        // kebabName
        $kebabName = end($kebabParts);
        // 模块类名
        $className = Str::studly($kebabName);

        # 控制器：
        //'Demo/Users',
        //'Demo/UserPhones',
        //'Demo/Test/TestField',
        //'Demo/Test/Dev/User'

        # 路由名 route_name (大驼峰命名)
        //"route_name": [
        //"DemoUsers",
        //"DemoUserPhones",
        //"DemoTestTestField",
        //"DemoTestDevUser"
        //],
        $data['route_name'] = $routeName;

        # 路由全路径 route_path_full (小写，中划线，路径)
        //"route_path_full": [
        //"/demo/users",
        //"/demo/user-phones",
        //"/demo/test/test-field",
        //"/demo/test/dev/user"
        //],
        $data['route_path_full'] = $routePathFull;

        # 路由 route_path (最后部分，小写，中划线)
        //"route_path": [
        //"users",
        //"user-phones",
        //"test-field",
        //"user"
        //],
        $data['route_path'] = $kebabName;

        # 类名 class_name (控制器最后部分，大驼峰命名)
        //"Users",
        //"UserPhones",
        //"TestField",
        //"User"
        $data['class_name'] = $className;

        # 类名 base_class_name (控制器最后部分，大驼峰命名 前加 Base)
        //"BaseUsers",
        //"BaseUserPhones",
        //"BaseTestField",
        //"BaseUser"
        $data['base_class_name'] = 'Base'.$className;

        # 组件路径  component (路径)
        //"demo/users/index",
        //"demo/user-phones/index",
        //"demo/test/test-field/index",
        //"demo/test/dev/user/index"
        $data['component'] = implode('/', $kebabParts) . "/index";

        # 权限代码 (各个部分用：隔开，相同的部分中划线隔开)
        //"demo:users",
        //"demo:user-phones",
        //"demo:test:test-field",
        //"demo:test:dev:user"
        $data['code_prefix'] = trim(str_replace('/', ':', $routePathFull), ':');

        # 方法权限代码前缀 (在权限代码的基础上加 冒号)
        //"demo:users:",
        //"demo:user-phones:",
        //"demo:test:test-field:",
        //"demo:test:dev:user:"
        $data['action_code_prefix'] = trim(str_replace('/', ':', $routePathFull), ':') . ':';

        # 表名 (全部下画线隔开)
        //"table_name": [
        //"demo_users",
        //"demo_user_phones",
        //"demo_test_test_field",
        //"demo_test_dev_user"
        //],
        $table_name = str_replace(['/', '-'], '_', trim($routePathFull, '/'));
        $data['table_name'] = preg_replace('/_{2,}/', '_', $table_name);

        # 多语言键名 （点号隔开）
        //"lang_prefix": [
        //"menu.demo.users",
        //"menu.demo.user-phones",
        //"menu.demo.test.test-field",
        //"menu.demo.test.dev.user"
        //],
        $data['lang_prefix'] = 'menu.' . trim(str_replace('/', '.', $routePathFull), '.');

        # 菜单多语言前缀
        //"menu_lang_prefix": [
        //"menu.demo.users.",
        //"menu.demo.user-phones.",
        //"menu.demo.test.test-field.",
        //"menu.demo.test.dev.user."
        //],
        $data['menu_lang_prefix'] = 'menu.' . trim(str_replace('/', '.', $routePathFull), '.') . '.';

        # 字段多语言键名前缀
        //"field_lang_prefix": [
        //"menu.demo.users.field.",
        //"menu.demo.user-phones.field.",
        //"menu.demo.test.test-field.field.",
        //"menu.demo.test.dev.user.field."
        //],
        $data['field_lang_prefix'] = 'menu.' . trim(str_replace('/', '.', $routePathFull), '.') . '.field.';

        # 方法多语言键名前缀
        //"action_lang_prefix": [
        //"menu.demo.users.action.",
        //"menu.demo.user-phones.action.",
        //"menu.demo.test.test-field.action.",
        //"menu.demo.test.dev.user.action."
        //],
        $data['action_lang_prefix'] = 'menu.' . trim(str_replace('/', '.', $routePathFull), '.') . '.action.';

        # url (点号隔开)
        //"url_prefix": [
        //"demo.users",
        //"demo.user-phones",
        //"demo.test.test-field",
        //"demo.test.dev.user"
        //],
        $data['url_prefix'] = implode('.', $kebabParts);

        # 控制器类
        //"controller_class": [
        //"controller\\demo\\Users",
        //"controller\\demo\\UserPhones",
        //"controller\\demo\\test\\TestField",
        //"controller\\demo\\test\\dev\\User"
        //],
        $data['controller_class'] = $module_path ?
            "controller\\{$module_path}\\{$className}" :
            "controller\\{$className}";

        # 控制器类别名
        //"controller_class_alias": [
        //"UsersController",
        //"UserPhonesController",
        //"TestFieldController",
        //"UserController"
        //],
        $data['controller_class_alias'] = $className . 'Controller';

        # 控制器实例
        //"controller_instance": [
        //"usersController",
        //"userPhonesController",
        //"testFieldController",
        //"userController"
        //],
        $data['controller_instance'] = Str::camel($className) . 'Controller';

        # 控制器相对路径
        //"controller_base_path": [
        //"controller\\demo\\BaseUsers.php",
        //"controller\\demo\\BaseUserPhones.php",
        //"controller\\demo\\test\\BaseTestField.php",
        //"controller\\demo\\test\\dev\\BaseUser.php"
        //],
        $data['controller_base_path'] = $module_path ?
            'controller' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "Base{$className}.php" :
            'controller' . DIRECTORY_SEPARATOR . "Base{$className}.php";

        # 控制器相对路径
        //"controller_path": [
        //"controller\\demo\\Users.php",
        //"controller\\demo\\UserPhones.php",
        //"controller\\demo\\test\\TestField.php",
        //"controller\\demo\\test\\dev\\User.php"
        //],
        $data['controller_path'] = $module_path ?
            'controller' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "{$className}.php" :
            'controller' . DIRECTORY_SEPARATOR . "{$className}.php";

        # 控制器命名空间
        //"controller_namespace": [
        //"controller\\demo",
        //"controller\\demo",
        //"controller\\demo\\test",
        //"controller\\demo\\test\\dev"
        //],
        $data['controller_namespace'] = $module_path ?
            'controller' . DIRECTORY_SEPARATOR . $module_path :
            'controller';

        # 模型类
        //"model_class": [
        //"model\\demo\\Users",
        //"model\\demo\\UserPhones",
        //"model\\demo\\test\\TestField",
        //"model\\demo\\test\\dev\\User"
        //],
        $data['model_class'] = $module_path ?
            "model\\{$module_path}\\{$className}" :
            "model\\{$className}";

        # 模型类别名
        //"model_class_alias": [
        //"UsersModel",
        //"UserPhonesModel",
        //"TestFieldModel",
        //"UserModel"
        //],
        $data['model_class_alias'] = $className . 'Model';

        # 模型实例
        //"model_instance": [
        //"usersModel",
        //"userPhonesModel",
        //"testFieldModel",
        //"userModel"
        //],
        $data['model_instance'] = Str::camel($className) . 'Model';

        # 模型相对路径
        //"model_base_path": [
        //"model\\demo\\BaseUsers.php",
        //"model\\demo\\BaseUserPhones.php",
        //"model\\demo\\test\\BaseTestField.php",
        //"model\\demo\\test\\dev\\BaseUser.php"
        //],
        $data['model_base_path'] = $module_path ?
            'model' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "Base{$className}.php" :
            'model' . DIRECTORY_SEPARATOR . "Base{$className}.php";

        # 模型相对路径
        //"model_path": [
        //"model\\demo\\Users.php",
        //"model\\demo\\UserPhones.php",
        //"model\\demo\\test\\TestField.php",
        //"model\\demo\\test\\dev\\User.php"
        //],
        $data['model_path'] = $module_path ?
            'model' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "{$className}.php" :
            'model' . DIRECTORY_SEPARATOR . "{$className}.php";

        # 模型命名空间
        //"model_namespace": [
        //"model\\demo",
        //"model\\demo",
        //"model\\demo\\test",
        //"model\\demo\\test\\dev"
        //],
        $data['model_namespace'] = $module_path ?
            'model' . DIRECTORY_SEPARATOR . $module_path :
            'model';

        # 验证类
        //"validate_class": [
        //"validate\\demo\\Users",
        //"validate\\demo\\UserPhones",
        //"validate\\demo\\test\\TestField",
        //"validate\\demo\\test\\dev\\User"
        //],
        $data['validate_class'] = $module_path ?
            "validate\\{$module_path}\\{$className}" :
            "validate\\{$className}";

        # 验证类别名
        //"validate_class_alias": [
        //"UsersValidate",
        //"UserPhonesValidate",
        //"TestFieldValidate",
        //"UserValidate"
        //],
        $data['validate_class_alias'] = $className . 'Validate';

        # 验证实例
        //"validate_instance": [
        //"usersValidate",
        //"userPhonesValidate",
        //"testFieldValidate",
        //"userValidate"
        //],
        $data['validate_instance'] = Str::camel($className) . 'Validate';

        # 验证器相对路径
        //"validate_base_path": [
        //"validate\\demo\\BaseUsers.php",
        //"validate\\demo\\BaseUserPhones.php",
        //"validate\\demo\\test\\BaseTestField.php",
        //"validate\\demo\\test\\dev\\BaseUser.php"
        //],
        $data['validate_base_path'] = $module_path ?
            'validate' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "Base{$className}.php" :
            'validate' . DIRECTORY_SEPARATOR . "Base{$className}.php";

        # 验证器相对路径
        //"validate_path": [
        //"validate\\demo\\Users.php",
        //"validate\\demo\\UserPhones.php",
        //"validate\\demo\\test\\TestField.php",
        //"validate\\demo\\test\\dev\\User.php"
        //],
        $data['validate_path'] = $module_path ?
            'validate' . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "{$className}.php" :
            'validate' . DIRECTORY_SEPARATOR . "{$className}.php";

        # 验证器命名空间
        //"validate_namespace": [
        //"validate\\demo",
        //"validate\\demo",
        //"validate\\demo\\test",
        //"validate\\demo\\test\\dev"
        //],
        $data['validate_namespace'] = $module_path ?
            'validate' . DIRECTORY_SEPARATOR . $module_path :
            'validate';

        # jsapi 文件
        //"vue_jsapi": [
        //"demo\\users.js",
        //"demo\\user-phones.js",
        //"demo\\test\\test-field.js",
        //"demo\\test\\dev\\user.js"
        //],
        $data['vue_jsapi'] = $module_path ?
            $module_path . DIRECTORY_SEPARATOR . "{$kebabName}.js" :
            "{$kebabName}.js";

        # view dir
        //"vue_views": [
        //"demo",
        //"demo",
        //"demo\\test",
        //"demo\\test\\dev"
        //]
        $data['vue_views'] = $module_path;

        # view dir
        //"vue_views": [
        //"demo\\users",
        //"demo\\user-phones",
        //"demo\\test\\test-field",
        //"demo\\test\\dev\\user"
        //]
        $data['views_dir'] = $module_path . '\\' .$kebabName;

        // 排序
        ksort($data);
        return $data;
    }
}